package com.lei.leetcode;

/**
 * @Author LeiXiaoLei
 * @Date 2022/8/29 19:53
 * @Version 1.0
 */
public class S_303 {
    private SegmentTree<Integer> segmentTree;

    public S_303(int[] nums) {
        if (nums.length > 0) {
            Integer[] data = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++) {
                data[i] = nums[i];
            }
            segmentTree = new SegmentTree<>(data, Integer::sum);
        }
    }

    public int sumRange(int left, int right) {
        if (segmentTree == null) throw new IllegalArgumentException("Segment Tree is null");
        return segmentTree.query(left, right);
    }

    @FunctionalInterface
    private interface Merger<E> {
        E merge(E a, E b);
    }

    private class SegmentTree<E> {

        private E[] tree;
        private E[] data;
        private Merger<E> merger;

        public SegmentTree(E[] arr, Merger<E> merger) {
            this.data = (E[]) new Object[arr.length];
            this.merger = merger;
            for (int i = 0; i < arr.length; i++) {
                data[i] = arr[i];
            }
            this.tree = (E[]) new Object[4 * arr.length];
            buildSegmentTree(0, 0, data.length - 1);
        }

        private void buildSegmentTree(int treeIndex, int l, int r) {
            if (l == r) {
                tree[treeIndex] = data[l];
                return;
            }
            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);

            int mid = l + (r - l) / 2;
            buildSegmentTree(leftTreeIndex, l, mid);
            buildSegmentTree(rightTreeIndex, mid + 1, r);

            tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
        }

        public E query(int queryL, int queryR) {
            if (queryL < 0 || queryR >= data.length || queryL > queryR)
                throw new IllegalArgumentException("Index is Illegal.");
            return query(0, 0, data.length - 1, queryL, queryR);
        }

        private E query(int treeIndex, int l, int r, int queryL, int queryR) {
            if (l == queryL && r == queryR) return tree[treeIndex];

            int mid = l + (r - l) / 2;
            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);

            if (queryL >= mid + 1) return query(rightTreeIndex, mid + 1, r, queryL, queryR);
            else if (queryR <= mid) return query(leftTreeIndex, l, mid, queryL, queryR);

            E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
            E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
            return merger.merge(leftResult, rightResult);
        }

        public int getSize() {
            return data.length;
        }

        public E get(int index) {
            if (index < 0 || index >= data.length)
                throw new IllegalArgumentException("Index is Illegal.");
            return data[index];
        }

        private int leftChild(int index) {
            return 2 * index + 1;
        }

        private int rightChild(int index) {
            return 2 * index + 2;
        }
    }
}
